#include <stdbool.h>
#include <stdlib.h>
#include "SqliteInterface.h"
#include "../allThread.h"
#include "../RealtimeWaveform.h"
#include "../ErrorLog.h"
#include "../modbus/create_modbus_custom_slave.h"


void MSD_MiscFileInit(void)
{
    int ret;
    ret = sqlite_db_open();
    if (ret == SQLITE_ERROR)
    {
        printf(" __DATE__: %s ,__TIME__: %s ,__FILE__: %s ,__FUNCTION__: %s ,__LINE__: %d sqlite_db_open error,\n",
               __DATE__, __TIME__, __FILE__, __func__, __LINE__);
        return;
    }
}


static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
    int i;
    for (i = 0; i < argc; i++)
    {
        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    printf("\n");
    return 0;
}


int MSD_MicroFileWrite(uint16_t microFileId, uint32_t innerId, struct commonData *pCommonData)
{
    int ret;

    char *zErrMsg = 0;
    char sql[200] = {0};
    char table_name[50] = "MicroFile";
    char dataValue[50];

    switch (pCommonData->cd_dataType)
    {
    case COM_DATA_TYPE_UINT8:
        sprintf(dataValue,"%d",pCommonData->cd_dataValue.uint8Value);
        break;
    case COM_DATA_TYPE_INT8:
        sprintf(dataValue,"%d",pCommonData->cd_dataValue.int8Value);
        break;
    case COM_DATA_TYPE_UINT16:
        sprintf(dataValue,"%d",pCommonData->cd_dataValue.uint16Value);
        break;
    case COM_DATA_TYPE_INT16:
        sprintf(dataValue,"%d",pCommonData->cd_dataValue.int16Value);
        break;
    case COM_DATA_TYPE_UINT32:
        sprintf(dataValue,"%d",pCommonData->cd_dataValue.uint32Value);
        break;
    case COM_DATA_TYPE_INT32:
        sprintf(dataValue,"%d",pCommonData->cd_dataValue.int32Value);
        break;
    case COM_DATA_TYPE_UINT64:
        sprintf(dataValue,"%lld",pCommonData->cd_dataValue.uint64Value);
        break;
    case COM_DATA_TYPE_INT64:
        sprintf(dataValue,"%lld",pCommonData->cd_dataValue.int64Value);
        break;
    case COM_DATA_TYPE_FLOAT:
        sprintf(dataValue,"%f",pCommonData->cd_dataValue.floatValue);
        break;
    default:
        break;
    }

    sprintf(sql, "UPDATE %s%d SET ParaType=%d,ParaValue=%s WHERE id=%d", table_name, microFileId, pCommonData->cd_dataType, dataValue, innerId);

    //printf("%s\n",sql);

    ret = sqlite3_exec(db, sql, callback, 0, &zErrMsg);

    if (ret != SQLITE_OK)
    {
        printf(" __DATE__: %s ,__TIME__: %s ,__FILE__: %s ,__FUNCTION__: %s ,__LINE__: %d sqlite3_exec SQL error, %s\n",
               __DATE__, __TIME__, __FILE__, __func__, __LINE__,zErrMsg);

        sqlite3_free(zErrMsg);
        return -1;
    }
    else
    {
        // fprintf(stdout, "Records update successfully\n");
    }
    return SQLITE_OK;
}

int MSD_MicroFileRead(uint16_t microFileId, uint32_t innerId, struct commonData *pCommonData)
{
    char *err;
    char **dataSql;
    int Total_number_of_rows, Total_number_of_column, current_row, current_column;
    char sql[200] = {0};
    char table_name[50] = "MicroFile";
    sprintf(sql, "select * from %s%d where id=%d", table_name, microFileId, innerId);

    if (0 != sqlite3_get_table(db, sql, &dataSql, &Total_number_of_rows, &Total_number_of_column, &err))
    {
        printf(" __DATE__: %s ,__TIME__: %s ,__FILE__: %s ,__FUNCTION__: %s ,__LINE__: %d sqlite3_get_table error,\n",
               __DATE__, __TIME__, __FILE__, __func__, __LINE__);
        return -1;
    }

    // printf("Total_number_of_rows : %d \n", Total_number_of_rows);
    // printf("Total_number_of_column : %d \n", Total_number_of_column);

    if (0 != Total_number_of_rows)
    {
        for (current_row = 0; current_row < Total_number_of_rows + 1; current_row++)
        {
            for (current_column = 0; current_column < Total_number_of_column; current_column++)
            {
                //  	printf("current_row = %d, current_column = %d \n",current_row,current_column);

                if (current_row > 0) // 第0行打印的表头，因此略过
                {

                    switch (current_column) // current_row = 0 // 只对应数据的每一列的名字（字段名、表头），实际数据应该是从1开始
                    {
                    case 0:
                        // id = atoi(*dataSql);
                        break;
                    case 1:
                        pCommonData->cd_dataType = atoi(*dataSql);
                        // printf("cd_dataType %s \n", *dataSql);
                        break;
                    case 2:
                        switch (pCommonData->cd_dataType)
                        {
                        case COM_DATA_TYPE_UINT8:
                            pCommonData->cd_dataValue.uint8Value = atoi(*dataSql);
                            break;
                        case COM_DATA_TYPE_INT8:
                            pCommonData->cd_dataValue.int8Value = atoi(*dataSql);
                            break;
                        case COM_DATA_TYPE_UINT16:
                            pCommonData->cd_dataValue.uint16Value = atoi(*dataSql);
                            break;
                        case COM_DATA_TYPE_INT16:
                            pCommonData->cd_dataValue.int16Value = atoi(*dataSql);
                            break;
                        case COM_DATA_TYPE_UINT32:
                            pCommonData->cd_dataValue.uint32Value = atoi(*dataSql);
                            break;
                        case COM_DATA_TYPE_INT32:
                            pCommonData->cd_dataValue.int32Value = atoi(*dataSql);
                            break;
                        case COM_DATA_TYPE_UINT64:
                            pCommonData->cd_dataValue.uint64Value = atoll(*dataSql);
                            break;
                        case COM_DATA_TYPE_INT64:
                            pCommonData->cd_dataValue.int64Value = atoll(*dataSql);
                            break;
                        case COM_DATA_TYPE_FLOAT:
                            pCommonData->cd_dataValue.floatValue = atof(*dataSql);
                            break;
                        default:
                            break;
                        }
                        // printf("pCommonData->cd_dataValue.int16Value %d \n", pCommonData->cd_dataValue.int16Value);

                        break;
                    default:
                        break;
                    }
                }
                dataSql++;
            }
        }
    }
    else
    {
        printf(" __DATE__: %s ,__TIME__: %s ,__FILE__: %s ,__FUNCTION__: %s ,__LINE__: %d \n %s : empty \n", __DATE__, __TIME__, __FILE__, __func__, __LINE__, sql);
        return -1;
    }
    return SQLITE_OK;
}
